/*
 * Created on Oct 30, 2003
 */
package com.unash.server.z3950Server;

/**
 * @author Ashwini Gokhale
 *
 */

import java.net.ServerSocket;
import java.net.Socket;
import java.net.UnknownHostException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

import com.unash.server.Channel;
import com.unash.server.ThreadPool;


public class Z3950Server {
	
	public static void main(String[] args) {
		ServerSocket    serverSocket = null;
		Socket          clientSocket = null;
		Channel	        workQueue    = null;
		ThreadPool      threadPool   = null;
				
		final int portNumber = 7777;
		final boolean useThreadPool = true;
		final int threadPoolSize = 10;
				
		try{
			workQueue = new Channel(threadPoolSize);
		} 
		catch(IllegalArgumentException e){
			System.out.println("Invalid Thread Pool Size.");
			System.exit(1);
		}
		
		//threadPool = new ThreadPool(workQueue,threadPoolSize);
		ExecutorService executor = Executors.newCachedThreadPool();
		
		try{
			//serverSocket = new ServerSocket(portNumber);
			serverSocket = new ServerSocket(portNumber);
			
			for(;;){
				clientSocket = serverSocket.accept();
								
				System.out.println("Accepting Connection");
								
				Z3950Connection z3950Connection = new Z3950Connection(clientSocket);
				//threadPool.execute(z3950Connection);
				executor.execute(z3950Connection);
				//z3950Connection.start();
				
			}//for(;;)
			
			//serverSocket.close();
		}//try
		catch (UnknownHostException e) {
			System.out.println("Could not listen on port : " + portNumber);
			System.exit(1);
		}
		catch(Exception e){
			System.out.println(e);
			System.exit(1);
		}//catch
			
	}//main
}//Z3950Server